﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace mrp16062010.Veri
{
    class OperasyonTanim : ConnectionImporter
    {
        private int operasyonTanimId;

        public int OperasyonTanimId
        {
            get { return operasyonTanimId; }
            set { operasyonTanimId = value; }
        }

        private string ad;

        public string Ad
        {
            get { return ad; }
            set { ad = value; }
        }

        public void Kaydet()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("InsertOperasyonTanim", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmReturnValue = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
            prmReturnValue.Direction = ParameterDirection.ReturnValue;

            #region Populate Parameters
            SqlParameter prmAd = cmd.Parameters.Add("@Ad", SqlDbType.NVarChar);
            prmAd.Direction = ParameterDirection.Input;
            prmAd.Size = 50;

            prmAd.Value = this.Ad;


            SqlParameter prmOperasyonTanimId = cmd.Parameters.Add("@OperasyonTanimId", SqlDbType.Int);


            prmOperasyonTanimId.Direction = ParameterDirection.Output;

            #endregion

            cmd.ExecuteNonQuery();
            this.OperasyonTanimId = (int)prmOperasyonTanimId.Value;
            cmd.Dispose();
            Disconnect();
        }

        public void Sil()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("DeleteOperasyonTanim", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmCizelgeId = cmd.Parameters.Add("@OperasyonTanimId", SqlDbType.Int);
            prmCizelgeId.Direction = ParameterDirection.Input;
            prmCizelgeId.Value = this.operasyonTanimId;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void Guncelle()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("UpdateOperasyonTanim", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmOperasyonTanimId = cmd.Parameters.Add("@OperasyonTanimId", SqlDbType.Int);
            prmOperasyonTanimId.Direction = ParameterDirection.Input;

            prmOperasyonTanimId.Value = this.OperasyonTanimId;


            SqlParameter prmAd = cmd.Parameters.Add("@Ad", SqlDbType.NVarChar);
            prmAd.Direction = ParameterDirection.Input;
            prmAd.Size = 50;

            prmAd.Value = this.Ad;


            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void Bul()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectOperasyonTanim", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmIsMerkeziId = cmd.Parameters.Add("@OperasyonTanimId", SqlDbType.Int);
            prmIsMerkeziId.Direction = ParameterDirection.Input;
            prmIsMerkeziId.Value = this.operasyonTanimId;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            foreach (DataRow row in dt.Rows)
            {
                this.operasyonTanimId = int.Parse(row["OperasyonTanimId"].ToString());
                this.ad = row["Ad"].ToString();
                break;
            }

            cmd.Dispose();
            Disconnect();
        }

        public List<OperasyonTanim> Listele()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectOperasyonTanimsAll", Connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<OperasyonTanim> liste = new List<OperasyonTanim>();
            foreach (DataRow row in dt.Rows)
            {
                OperasyonTanim yeniOpTan = new OperasyonTanim();
                yeniOpTan.operasyonTanimId = int.Parse(row["OperasyonTanimId"].ToString());
                yeniOpTan.ad = row["Ad"].ToString();
                liste.Add(yeniOpTan);
            }

            cmd.Dispose();
            Disconnect();
            return liste;
        }
    }
}